local M = require "packages/mamashengde/record/Made"

local jinghong = fk.CreateSkill {
  name = "shine__jinghong",
}

Fk:loadTranslationTable {
  ["shine__jinghong"] = "惊鸿",
  [":shine__jinghong"] = "称号与你相同的角色不因此技能获得牌后，你可以消耗1点魔力令其判定，若为黑色牌其获得并重复之；你的黑色牌无次数限制且不计入手牌上限。",
  ["#jinghong-invoke"] = "惊鸿：你可以消耗1点魔力令 %dest 进行“洛神”判定",
  ["#jinghong-invoke-multi"] = "惊鸿：你可以消耗1点魔力令其中一名角色进行“洛神”判定",
}

jinghong:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(jinghong.name) and M.getMaryoku(player) > 0 then
      for _, move in ipairs(data) do
        if move.to ~= nil and move.toArea == Card.PlayerHand and move.skillName ~= jinghong.name then
          if M.getHonor(move.to) == M.getHonor(player) and not move.to.dead then
            return true
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.to ~= nil and move.toArea == Card.PlayerHand and move.skillName ~= jinghong.name then
        if M.getHonor(move.to) == M.getHonor(player) and not move.to.dead then
          table.insertIfNeed(targets, move.to)
        end
      end
    end
    if #targets == 1 then
      if room:askToSkillInvoke(player, {
        skill_name = jinghong.name,
        prompt = "#jinghong-invoke::"..targets[1].id,
      }) then
        event:setCostData(self, {tos = targets})
        return true
      end
    elseif #targets > 1 then
      local to = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#jinghong-invoke-multi",
        skill_name = jinghong.name,
      })
      if #to > 0 then
        event:setCostData(self, {tos = to})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    M.changeMaryoku(player, -1 ,jinghong.name)
    local to = event:getCostData(self).tos[1]
    while true do
      local judge = {
        who = to,
        reason = jinghong.name,
        pattern = ".|.|black",
      }
      room:judge(judge)
      if not judge:matchPattern() or to.dead then
        break
      end
    end
  end,
})

jinghong:addEffect(fk.FinishJudge, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.reason == jinghong.name and
      data.card.color == Card.Black and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function(self, event, target, player, data)
    player.room:obtainCard(player, data.card, true, fk.ReasonJustMove, player, jinghong.name)
  end,
})

jinghong:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(jinghong.name) and card and card.color == Card.Black
  end,
})

jinghong:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return player:hasSkill(jinghong.name) and card.color == Card.Black
  end,
})

return jinghong